-- This script creates a visual life bar out of a row of lights.
-- Algorithm: Every loop iteration, check to see if life has changed
--            If so, loop through all the lights and set each
--              according to how much life the player has left
--            In doing so, darken any lights that represent more life
--              than the player has.
--
--
-- This script also does some cool procedure stuff and arms some
-- weapons.  
-- Version: 3

_report_errors true

_procedure init  -- Init script start
x: def 0         -- x, a counter variable
tag: def 0       -- tag, a counter pointing to the current tag
start: def 3     -- the first tag in the set
num: def 15      -- the last tag in the set
life: def 0      -- the player's life
oldlife: def 0   -- the player's life last time we checked

set_oxygen 1000              -- prepare player oxygen/life
set_life 250
Add_Item plasma_magazine     -- Arm the player w/ fusion pistol
Add_Item plasma_pistol
Select_Weapon plasma_pistol  -- And make sure it's selected

end              -- Init script end

_procedure idle
get_life life                   -- check to see if the life has changed
if_!= life, oldlife, doupdate   -- if so, jump to doupdate
get_tag_state 1, x              -- check to see if switch has been clicked
if_= x, TRUE, hurtme
get_tag_state 2, x
if_= x, TRUE, armme
end                             -- otherwise loop

doupdate: call update           -- call update to update the lights
end                             -- loop

update:  block_start            -- execute this block in one frame
set oldlife, life               -- set oldlife to life for next loop
set x, 1                        -- we start with value 1
set tag, start                  -- with the first light
for: if_< life, x, dark         -- if life < x then dim the light
set_tag_state, tag, TRUE        -- otherwise turn the light on
jump next                       -- and jump to the next light
dark: set_tag_state, tag, FALSE -- turn light off
next: add x, 11                 -- add 12 to x
add tag, 1                      -- go to next light
if_> tag, num, done             -- if we ran out of lights, we are done
jump for                        -- otherwise loop
done: block_end 
end                             -- when we are finished, return

hurtme: inflict_damage 12       -- hurt the player
end                             -- return to the loop

armme: block_start              -- execute this block in one frame
Add_Item assault_rifle          -- deck the player out
Add_Item assault_rifle_magazine
Add_Item assault_grenade_magazine
Add_Item plasma_magazine
Add_Item alien_shotgun
Select_Weapon alien_shotgun     -- select the alien weapon
get_life x
add x, 50                       -- add 50 more health to the player
set_life x
set_tag_state 2, FALSE          -- turn the switch off
block_end
end                             -- return to the loop
